<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2019/1/15
 * Time: 13:48
 */
namespace app\shop\controller;
use think\Controller;
use think\Db;
use think\View;

class Base extends Controller
{
    /**
     * 初始化监听登录状态
     */
    public function _initialize()
    {
        if(!session('?shop_id')){
            $this->redirect(url('shop/login/indexLogin'));
        }else{
            $shop_status = Db::table('up_shops')
                ->where('s_id',session('shop_id'))
                ->value('s_sta');
            if($shop_status == 2){
                $this->redirect(url('shop/login/indexLogin'));
            }
            $shop_son = Db::name('role_name')
                ->where('role_user',session('login_phone'))
                ->value('role_status');
            if($shop_son && $shop_son == 2){
                $this->redirect(url('shop/login/indexLogin'));
            }
        }
        //公共数据
        $info = Db::table('up_shops')->where('s_id', session('shop_id'))->find();
        $info['wei'] = 0;
        $wherebase = [
            'n_uors_id' =>session('shop_id')
        ];
        if(session('shop_auth') != 200){
            $wherebase['n_title'] = ['<>','代理商审核通过'];
        }
        $wei = Db::table('up_notice')
            ->where($wherebase)
            ->where('n_read', 0)
            ->field('n_id')
            ->find();
        $info['shop_auth'] = session('shop_auth');
        if($wei){
            $info['wei'] = 1;
        }
        $view = new View();
        $view::share("info",$info);
    }

    public function search($search, $page, $fl = 'search')
    {
        if ($search === null && $page === null) {
            session($fl, null);
        } elseif ($search === '') {
            session($fl, null);
        } elseif ($search !== null) {
            session($fl, serialize($search));
        }
        return unserialize(session($fl));
    }
    /**
     * 压缩图片
     * @param $imgsrc:原图片地址（完整的图片路径）
     * @param $imgdst:压缩后的图片地址，也是完整的
     */
    function image_png_size_addshop($imgsrc,$imgdst){
        list($width,$height,$type)=getimagesize($imgsrc);
        switch($type){
            case 1:
                break;
            case 2:
                header('Content-Type:image/jpeg');
                $image = imagecreatefromjpeg($imgsrc);
                imagejpeg($image, $imgdst,50);
                imagedestroy($image);
                break;
            case 3:
                header('Content-Type:image/png');
                $image = imagecreatefrompng($imgsrc);
                imagejpeg($image, $imgdst,50);
                imagedestroy($image);
                break;
        }
    }
    public function hecheng_pic($dst_path)
//    public function hecheng_pic()
    {
//        $dst_path='http://cloud.xuanwumobile.com/uploads/shop/images/20201027/c2de304ebd5a4e194f5b53c95ea81e75.jpeg';
        $logo = ROOT_PATH . 'public' . DS . 'static/shop/image/logo.png';
        $src_path = ROOT_PATH . 'public' . DS . 'static/shop/image/guanzhu.png';
        $src_path2 = ROOT_PATH . 'public' . DS . 'static/shop/image/xiazai.png';
        $imgs    = array();
        $imgs[0] = $dst_path;
        $imgs[1] = $src_path;
        $imgs[2] = $src_path2;
        list($bg_w,$bg_h) = getimagesize($dst_path);
        $target_img = imagecreatetruecolor($bg_w,$bg_h+250); // 背景图片
        $color = imagecolorallocate($target_img, 99, 158, 236); // 为真彩色画布创建白色背景，再设置为透明
        imagefill($target_img, 0, 0, $color);
        imageColorTransparent($target_img, $color);

        $source = array();

        foreach ($imgs as $k => $v) {
            $ename = getimagesize($v);
            $ename = explode('/',$ename['mime']);
            $ext = $ename[1];
            switch($ext) {
                case 'jpg':
                    $source[$k]['source'] = Imagecreatefromjpeg($v);
                    $source[$k]['size'] = getimagesize($v);
                    break;
                case 'jpeg':
                    $source[$k]['source'] = Imagecreatefromjpeg($v);
                    $source[$k]['size'] = getimagesize($v);
                    break;
                case 'png':
                    $source[$k]['source'] = Imagecreatefrompng($v);
                    $source[$k]['size'] = getimagesize($v);
                    break;
                case 'gif':
                default:
                    $source[$k]['source'] = Imagecreatefromgif($v);
                    $source[$k]['size'] = getimagesize($v);
                    break;
            }
        }

//        -------------加文字---------------
        $fontfont = ROOT_PATH . 'public' . DS . 'static/shop/js/msyh.ttf';
        $colorcolor = imagecolorallocate($target_img, 255, 255, 255); // 为真彩色画布创建白色背景，再设置为透明
        $text = '云屏，开启领钱时代';
//        $text = iconv('utf-8', 'gbk', $text);
        $text = mb_convert_encoding($text, "html-entities", "utf-8");
        imagettftext($target_img, 60, 0, 620, $bg_h+155, $colorcolor, $fontfont, $text);

        $hz = substr(strrchr($dst_path, '.'), 1);
        $today = date('Ymd');
        $path = ROOT_PATH . 'public' . DS . 'uploads/images/'.$today.'/';
        if(!file_exists($path)){
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($path, 0700,true);
        }
        //生成新图片名
        $name = date("YmdHis").rand(1000,9999).".".$hz;
        $image = $path.$name;
        $url =  http_type ();
        $sec_image = $url . '/uploads/images/' .$today.'/'.$name;
        $tmp  = 0;
        $tmpy = 0; //图片之间的间距
        for ($i = 0; $i < 3; $i++) {
            imagecopy($target_img, $source[$i]['source'], $tmp, $tmpy, 0, 0, $source[$i]['size'][0], $source[$i]['size'][1]);
            $tmp = 0;
            //如果是三张图
            if($i == 1){
                $tmp = $source[1]['size'][0]+30;
            }
            $tmpy = $source[0]['size'][1];
        }
        imagecopy($target_img, Imagecreatefromjpeg($logo), 494, $tmpy+71, 0, 0, 108, 108);
        Imagejpeg($target_img, $image);
        return $sec_image;
    }
    /**
     * 极光推送
     * @param $type 1:推送个人  2:推送全体
     * @param $alias  //user_id OR token别名
     * @param $message //推送消息内容
     * @param $extras //扩展字段接受数组
     * @return array
     */
    public function Jpush($type,$alias,$message,$extras)
    {
//        $alias = (string)$alias;
        import('jpush.autoload', EXTEND_PATH);
        //初始化JPushClient
        $client = new \JPush\Client(config('jpush.appKey'),config('jpush.masterSecret'));
        //生成推送Payload构建器
        $push = $client->push();
        //推送平台 'all'  OR  ['ios','android']  OR  'ios','android'
        $push->setPlatform('all');
        //1:推送个人  2:推送全体
        if($type==1){
            $push->addAlias($alias);//按别名推送
        }else{
            $push->addAllAudience();//广播消息推送
        }
        //IOS推送
        $push->iosNotification($message, [
                'alert'=>$message,
                'badge' => '+1',
                'extras' => $extras,
                'sound'=>'default'
            ]
        );
        //Android推送
        $push->androidNotification($message, [
                'alert'=>$message,
                'extras' => $extras
            ]
        );
        return $push->send();
    }
}